<?php
declare (strict_types=1);

namespace app\admin\service\user;

use app\admin\logic\user\Group;
use app\admin\logic\user\Permission as PermissionLogic;
use app\common\service\BaseService;
use app\school\logic\school\admin\SchoolDomain;
use app\school\model\school\School;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 *
 */
class Permission extends BaseService
{
    public function index()
    {
        $data = app(PermissionLogic::class)->getList();
        // 定义字段
        $keyList = [
            // 'id'          => ListItem::make()->title('ID'),
            'title'       => ListItem::make()->title('名称'),
            'permission'  => ListItem::make()->title('节点名称'),
            'is_menu'     => ListItem::make()->title('是否为导航'),
            'remark'      => ListItem::make()->title('节点备注'),
            'update_time' => ListItem::make()->title('更新时间'),
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 获取子节点列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-17
     */
    public function getChildrenList($pid)
    {
        $data = app(PermissionLogic::class)->getList($pid);

        return $this->parseToData($data, 1);
    }

    /**
     * 创建节点
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-19
     *
     * @return   [type]                         [description]
     */
    public function create()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $user = app(PermissionLogic::class)->createPermission($data);

            return $this->parseToData($user->toArray(), 1, '创建成功');
        }

        // 创建课程界面配置
        // 定义字段
        $keyList = [
            'permission' => FormItem::make()->title('节点标识')->required(),
            'title'      => FormItem::make()->title('节点名称')->required(),
            'is_menu'    => FormItem::make('slideSwitch')->title('是否为导航节点')->activeText('是')->activeValue(1)->inactiveText('否')->inactiveValue(0),
            'remark'     => FormItem::make('textarea')->placeholder('可输入100字的备注说明')->title('备注')->maxlength(100)->wordLimit()->rows(4)
        ];
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $keyList;
        // 提交方式
        $form->saveMethod  = 'POST';
        $form->savePostUrl = '/admin/user/permission';

        return $form->send();
    }

    /**
     * 编辑节点
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-19
     *
     * @param    [type]                         $id [description]
     * @return   [type]                             [description]
     */
    public function edit($id)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $user = app(PermissionLogic::class)->editPermission($data);

            return $this->parseToData($user->toArray(), 1, '编辑成功');
        }
        $datas = app(PermissionLogic::class)->where('id', $id)->findOrEmpty();
        // 创建课程界面配置
        // 定义字段
        $keyList = [
            'permission' => FormItem::make()->title('节点标识')->required(),
            'title'      => FormItem::make()->title('节点名称')->required(),
            'is_menu'    => FormItem::make('slideSwitch')->title('是否为导航节点')->activeText('是')->activeValue(1)->inactiveText('否')->inactiveValue(0),
            'remark'     => FormItem::make('textarea')->placeholder('可输入100字的备注说明')->title('备注')->maxlength(100)->wordLimit()->rows(4),
        ];
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->datas       = $datas;
        $form->pageKey     = $keyList;
        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/admin/user/permission/' . $id;

        return $form->send();
    }

    /**
     * 删除节点
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-17
     *
     * @param int $id [description]
     * @return   [type]                             [description]
     */
    public function delete(int $id)
    {
        app(PermissionLogic::class)->deletePermission($id);

        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 加载用户权限节点
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-22
     *
     * @return   [type]                         [description]
     */
    public function load()
    {
        $nodes = app(Group::class)->loadRuleList(request()->user);
        $mhmId = request()->mhm_id;
        // 域名
        if ($mhmId === 1 || config('eduline.appinfo.mhm', false) === false) {
            // 取配置的域名
            $effectiveDomain = config('eduline.appinfo.pc_host', '');
        } else {
            $effectiveDomain = app(SchoolDomain::class)->getEffectivePcDomain($mhmId);
        }
        // 机构code
        // $schoolCode = app(School::class)->where('id', $mhmId)->value('school_code');
        // 返回数据
        $ret = [
            'permission_nodes' => $nodes,
            'website'          => [
                'effective_domain' => $effectiveDomain,
                // 'school_code'      => $schoolCode
            ]
        ];

        return $this->parseToData($ret, 1);
    }

    /**
     * 获取树形结构
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-22
     *
     * @return   [type]                         [description]
     */
    public function getTreeList()
    {
        $list = app(PermissionLogic::class)->getTreeList();

        return $this->parseToData($list, 1);
    }

}
